﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FluorineFx;
using System.Runtime.Serialization.Formatters.Binary;
using Game.Server.Socket;

namespace Game.Server.MainApp
{
    public partial class MainApp : Form
    {
        private SocketServer _socketServer;

        public MainApp()
        {
            InitializeComponent();
            Main();
        }

        public void Main()
        {
            try
            {
                AllowDomain allowDomain = new AllowDomain();
                allowDomain.Start();

                _socketServer = new SocketServer(8888, 2000);
                _socketServer.ServerFullEvent += new SocketEvent(ServerFull);//服务器已满事件
                _socketServer.ClientConnectEvent += new SocketEvent(ClientConnect);//新客户端连接事件
                _socketServer.ClientCloseEvent += new SocketEvent(ClientClose);//客户端关闭事件
                _socketServer.SocketDataEvent += new SocketEvent(ReceiveData);//接收到数据事件
            }
            catch (Exception ex)
            {
                listSystem.Items.Add("错误:" + ex.Message);
            }
        }

        private void ClientConnect(object sender, SocketEventArgs e)
        {
            //listUser.Items.Add(e.Client.ClientSocket.RemoteEndPoint.ToString() + ", " + e.Client.ID + "," + e.Client.ClientSocket.Handle);//将当前连接者加到界面中的在线列表
        }

        //服务器已满,关闭已连接的客户端
        private void ServerFull(object sender, SocketEventArgs e)
        {
            e.Session.Close();
        }

        /// <summary>
        /// 关闭客户端连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClientClose(object sender, SocketEventArgs e)
        {
            _socketServer.SessionTable.Remove(e.Session.ID);
        }

        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReceiveData(object sender, SocketEventArgs e)
        {
            SocketServer socketServer = (SocketServer)sender;
            SocketPack socketPack = e.SocketPack;
            if (socketPack == null) return;

            switch (socketPack.RootPackCode)
            {
                case (int)DataPackCode.UserBaseCode.ROOT_CODE:
                    UserBase userBase = new UserBase(socketServer, e.Session, socketPack);
                    break;
            }
        }

        /// <summary>
        /// 运行服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (_socketServer.IsRun)
            {
                listSystem.Items.Add("服务器已经运行了！");
                return;
            }
            _socketServer.Start();
            listSystem.Items.Add(_socketServer.ServerSocket.LocalEndPoint.ToString());
        }

        /// <summary>
        /// 停运服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (!_socketServer.IsRun)
            {
                listSystem.Items.Add("服务器已经停止了！");
                return;
            }
            _socketServer.Stop();
            listSystem.Items.Add("服务器停止");
        }

        /// <summary>
        /// 刷新在线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            listUser.Items.Clear();
            if (!_socketServer.IsRun) return;
            int i = 0;
            foreach (Session Client in _socketServer.SessionTable.Values)
            {
                if (Client != null)
                {
                    listUser.Items.Add(Client.ClientSocket.RemoteEndPoint.ToString() + ", " + Client.ID + "," + Client.ClientSocket.Handle);
                }
                i++;
            }
        }
    }
}
